#include <e.h>
#include "e_mod_main.h"


struct _E_Config_Dialog_Data
{
	/*- BASIC -*/
	/*List of themes that are in /themes dir*/
    Evas_List *tlist;
    /*dafault theme that will be used when new widget cretaed*/
    const char *theme;
    /*dafault alpha that would be set for new widget*/
    int alpha;
    /*dafault alpha that would be set for new widget*/
    char *format ;
    
	E_Config_Dialog *cfd;
	Widget *w;
};

struct _E_Mod_Theme_Data
{
   E_Config_Dialog *cfd;
   const char *theme;
   const char *file;
   void *data;
   Evas_Object *text;
};

typedef struct _E_Mod_Theme_Data E_Mod_Theme_Data;

/* PROTOTYPES - same all the time */
static void *_create_data(E_Config_Dialog *cfd);
static void _free_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static int _basic_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static int _advanced_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static Evas_Object *_basic_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
static Evas_Object *_advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
static void _fill_data(E_Config_Dialog_Data *cfdata);

static void _tlist_cb_change(void *data);

/* a nice easy setup function that does the dirty work */
EAPI E_Config_Dialog *
_clock_widget_config(Widget *w)
{
	E_Config_Dialog *cfd;
	E_Config_Dialog_View *v;
	E_Container *con;
	char buf[4096];
	if (!w)
   		return NULL;
	   
	v = E_NEW (E_Config_Dialog_View, 1);
  	if (v == NULL)
    {
      warn ("malloc()");
      return;
    }
   
   /* methods */
	v->create_cfdata           = _create_data;
	v->free_cfdata             = _free_data;
	v->basic.apply_cfdata      = _basic_apply_data;
	v->basic.create_widgets    = _basic_create_widgets;
	v->advanced.apply_cfdata   = _advanced_apply_data;
	v->advanced.create_widgets = _advanced_create_widgets;
	/* create config diaolg for NULL object/data */
	snprintf (buf, sizeof (buf), "%s/module.edj",e_module_dir_get (w->m));
  	con = e_container_current_get (e_manager_current_get ());
	cfd = e_config_dialog_new(con,
			     _(MODULE_NAME" Settings"),
			    "E", MODULE_NAME,
			     buf, 0, v, w);
   return cfd;
}

/**--CREATE--**/

static void *
_create_data(E_Config_Dialog *cdd)
{
	Widget *w; 
   E_Config_Dialog_Data *cfdata;
   
   if (!cdd)
    return NULL;

  w = cdd->data;
  if (!w)
  	return NULL;
  cfdata = E_NEW(E_Config_Dialog_Data, 1);
  cfdata->cfd=cdd;
  cfdata->w=w;
  _fill_data(cfdata);
   return cfdata;
}

static void
_fill_data(E_Config_Dialog_Data *cfdata)
{
	Widget *w;
	Ecore_List *themes;
	char *themefile;
	const char *themename=NULL;
	const char fulltheme[PATH_MAX];
	const char *e_mod_themes_dir;
	char *a;
	char buf[PATH_MAX];
	Ecore_Evas *eebuf;
    Evas *evasbuf;
	Evas_Object *o2 = NULL;
	eebuf = ecore_evas_buffer_new(1, 1);
    evasbuf = ecore_evas_get(eebuf);
    o2 = edje_object_add(evasbuf);
	w=cfdata->w;
	

	snprintf (buf, sizeof (buf), "%s/%s",e_module_dir_get (w->m),"themes");
	e_mod_themes_dir=evas_stringshare_add(buf);
	if (ecore_file_is_dir(e_mod_themes_dir))
		themes = ecore_file_ls(e_mod_themes_dir);
		
	while ((themefile = ecore_list_next(themes)))
	{
		snprintf(fulltheme, sizeof(fulltheme), "%s/%s", e_mod_themes_dir, themefile);
		if (ecore_file_is_dir(fulltheme))
			continue;
	    if (!edje_object_file_set(o2, fulltheme, "modules/"MODULE_NAME"/main"))
	    	continue;
		E_Mod_Theme_Data *tdata;	
		tdata = E_NEW(E_Mod_Theme_Data, 1);
		tdata->cfd = cfdata->cfd;
		tdata->file = evas_stringshare_add(fulltheme);
		tdata->theme = evas_stringshare_add(themefile);
		tdata->data = NULL;
		cfdata->tlist=evas_list_append(cfdata->tlist,tdata);
	}
	ecore_list_destroy(themes);
	evas_object_del(o2);
    ecore_evas_free(eebuf);
    evas_stringshare_del(e_mod_themes_dir);
    
   /*- ADVANCED -*/
   cfdata->theme = evas_stringshare_add(cfdata->w->conf->theme);
   cfdata->alpha=cfdata->w->conf->alpha;
   cfdata->format = strdup(cfdata->w->conf->format);
}

static void
_free_data(E_Config_Dialog *cdd, E_Config_Dialog_Data *cfdata)
{
	Evas_List *tlist;
	E_Mod_Theme_Data *tdata;
	if (!cfdata)
		return;
	for (tlist = cfdata->tlist; tlist; tlist = tlist->next)
   	{	
		tdata=tlist->data;
		evas_stringshare_del(tdata->theme);
		evas_stringshare_del(tdata->file);
		E_FREE(tdata);
	}
	
	E_FREE(cfdata->format);
	cfdata->w->config_dialog = NULL;
	 /* Free the cfdata */
	E_FREE(cfdata);
}

/**--APPLY--**/
static int
_basic_apply_data(E_Config_Dialog *cdd, E_Config_Dialog_Data *cfdata)
{
	Widget *w;
   	w=cfdata->w;
   	evas_stringshare_del(w->conf->format);
   	w->conf->format=evas_stringshare_add(cfdata->format);
   	e_config_save_queue();
   	_widget_config_updated(w);
   	return 1; /* Apply was OK */
   	
}

static int
_advanced_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
{
	Widget *w;
   	w=cfdata->w;
   	evas_stringshare_del(w->conf->theme);
   	w->conf->theme=evas_stringshare_add(cfdata->theme);
   	w->conf->alpha=cfdata->alpha;
	e_config_save_queue();
	_widget_config_updated(w);
   	return 1; /* Apply was OK */
}

/**--GUI--**/

static Evas_Object *
_basic_create_widgets(E_Config_Dialog *cdd, Evas *evas, E_Config_Dialog_Data *cfdata)
{
	Evas_Object *o, *il,*il1, *of, *ot,*os,*ob,*txt;
	Evas_List *tlist;
	E_Mod_Theme_Data *tdata;
	int i=0;
	int selnum=0;
 	o = e_widget_list_add (evas, 0, 0);
   
   of = e_widget_frametable_add(evas, D_("Format Settings"), 1);
   ob = e_widget_entry_add (evas, &cfdata->format, NULL, NULL, NULL);
    e_widget_min_size_set (ob, 150, 1);
   e_widget_frametable_object_append(of, ob, 0, 1, 1, 1, 1, 0, 1, 0);
   ob=e_widget_label_add (evas, D_ ("Consult strftime(3) for format syntax"));
   e_widget_frametable_object_append(of, ob, 0, 2, 1, 1, 1, 0, 1, 0);
   e_widget_list_object_append(o, of, 1, 1, 0.5);
	return o;
	
}

static Evas_Object *
_advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata)
{
	Evas_Object *o, *il,*il1, *of, *ot,*os,*ob,*txt;
	Evas_List *tlist;
	E_Mod_Theme_Data *tdata;
	int i=0;
	int selnum=0;
	
	Evas_Textblock_Style *tb_style;
	
	tb_style = evas_textblock_style_new();
  	evas_textblock_style_set(tb_style,
    "DEFAULT='font=Vera font_size=10 align=left color=#000000ff wrap=line'" "br='\n'" "header=' '" "contents=' '");
	
 	o = e_widget_table_add(evas, 0);
    of = e_widget_frametable_add(evas, D_("Themes"), 0);
    txt = e_widget_textblock_add(evas);		
    e_widget_textblock_plain_set(txt,"");
	e_widget_min_size_set(txt, 180, 100);
   	i=0;selnum=0;
	il = e_widget_ilist_add(evas, 100, 100, NULL);
    for (tlist = cfdata->tlist; tlist; tlist = tlist->next)
   	{
   		
   		tdata=tlist->data;
   		tdata->data=cfdata;
//   		printf("%s\n",tdata->theme);
   		char *noext;
   		noext = ecore_file_strip_ext(tdata->theme);
   		tdata->text=txt;
   		e_widget_ilist_append(il, NULL,noext ,_tlist_cb_change, tdata, NULL);	
   		if (strcmp(tdata->theme,cfdata->theme)==0)
			selnum=i;
		i++;
		E_FREE(noext);
   	}
   e_widget_min_size_set(il, 180, 100);
   e_widget_ilist_go(il); 
   e_widget_frametable_object_append(of, il, 0, 0, 1, 2, 1, 1, 1, 1);
	e_widget_frametable_object_append(of, txt, 1, 0, 1, 2, 1, 1, 1, 1);
   if (selnum >= 0)
		e_widget_ilist_selected_set(il, selnum);
		
	e_widget_table_object_append(o, of, 0,3 , 1, 2, 1, 1, 1, 1);
	
   
 	
   of = e_widget_frametable_add(evas, D_("Appearance"), 1);
   ob = e_widget_label_add(evas, D_("Alpha"));
   e_widget_frametable_object_append(of, ob, 0, 0, 2, 1, 1, 1, 1, 1);
   ob = e_widget_slider_add(evas, 1, 0, _("%1.0f"), 0.0, 255, 1.0, 0,
                            NULL, &(cfdata->alpha), 130);
   e_widget_frametable_object_append(of, ob, 0, 1, 3, 1, 1, 0, 1, 0);
   e_widget_table_object_append(o, of, 0, 5, 1, 2, 1, 1, 1, 1);
	return o;
}

static void
_tlist_cb_change(void *data)
{
	E_Mod_Theme_Data *tdata;
	
   	E_Config_Dialog_Data *cfdata;
   	Evas_List *l;
	tdata=data;
	cfdata=tdata->data;
   const char *tmp;
   char buf[1024];
   char *text;
   char *name=NULL,*author=NULL,*version=NULL,*contact=NULL,*copyright=NULL;   
   if(!(name=edje_file_data_get(tdata->file,"name")))
   		name=strdup(" ");
   if(!(author=edje_file_data_get(tdata->file,"author")))
   		author=strdup(" ");
   if(!(version=edje_file_data_get(tdata->file,"version")))
   		version=strdup(" ");
   if(!(contact=edje_file_data_get(tdata->file,"contact info")))
   		contact=strdup(" ");
   if(!(copyright=edje_file_data_get(tdata->file,"copyright")))
	   	copyright=strdup(" ");
   if (tdata->text)
   {
   		e_widget_textblock_plain_set(tdata->text,"");
   		snprintf(buf,sizeof(buf),"Theme Name: %s\nAuthor: %s\nVersion: %s\nContact Info: %s\nCopyright:%s\n",name,author,version,contact,copyright);
   		text=strdup(buf);
   		e_widget_textblock_plain_set(tdata->text,text);
   		E_FREE(text);
   }
    E_FREE(name);
    E_FREE(author);
    E_FREE(version);
    E_FREE(contact);
    E_FREE(copyright);
    
	if (strcmp(cfdata->theme,tdata->theme)!=0)
	{
		evas_stringshare_del(cfdata->theme);
		cfdata->theme=evas_stringshare_add(tdata->theme);
	}

}







